package com.wontube.client.extension.utils;

import java.util.Collection;

/**
 * 数组相关的工具类
 * @author tanggf(2012-3-28)
 *
 */
public class ArrayUtils {
	/**
	 * 判断字符串数组中是否存在值
	 * @param arr
	 * @param val
	 * @return
	 */
	public static boolean contains(String[] sources, String val){
		if(val == null || sources == null || sources.length < 1){
			return false;
		}
		
		for (String in : sources) {
			if(val.equals(in)) {
				return true;
			}
		}
		return false;
	}
	
	public static String join(String[] sources, String separator){
		if(sources == null || sources.length == 0 || separator == null){
			return null;
		}
		
		StringBuilder sb = new StringBuilder();
		for (String s : sources) {
			sb.append(s).append(separator);
		}
		return sb.substring(0, sb.length() - separator.length());
	}
	
	public static String join(Collection<String> sources, String separator){
		if(sources == null || sources.size() == 0 || separator == null){
			return null;
		}
		
		StringBuilder sb = new StringBuilder();
		for (String s : sources) {
			sb.append(s).append(separator);
		}
		return sb.substring(0, sb.length() - separator.length());
	}
}
